package com.xoa.service.party;

import com.xoa.dao.party.ThreeFormMapper;
import com.xoa.model.party.ThreeForm;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2017/11/25.
 */
@Service
public class ThreeFormService {

    @Resource
    private ThreeFormMapper threeFormMapper;
    /*
      添加
     */
    public ToJson<ThreeForm> insert(String beginTime, String buildTime, String tbr, String lxdh, String tbsj , ThreeForm threeForm){
        ToJson<ThreeForm> json = new ToJson<>();
        try{
            if(beginTime != null && beginTime !=""){
                Date date= DateFormat.getDate(beginTime);
                threeForm.setOpenTime(date);
            }
            if(buildTime != null && buildTime !=""){
                Date date= DateFormat.getDate(buildTime);
                threeForm.setBuildFireLastTime(date);
            }
            threeForm.setFillingMan(tbr);
            threeForm.setContactNumber(lxdh);
            threeForm.setReportTime(tbsj);
            threeFormMapper.insert(threeForm);
            json.setMsg("ok");
            json.setFlag(0);

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
/*
修改
 */
    public ToJson<ThreeForm> update(String beginTime, String buildTime, String tbr, String lxdh, String tbsj, ThreeForm threeForm){
        ToJson<ThreeForm> json = new ToJson<>();
        try {
            if(beginTime != null && beginTime !=""){
                Date date= DateFormat.getDate(beginTime);
                threeForm.setOpenTime(date);
            }
            if(!StringUtils.checkNull(buildTime)){
                Date date= DateFormat.getDate(buildTime);
                threeForm.setBuildFireLastTime(date);
            }
            threeForm.setFillingMan(tbr);
            threeForm.setContactNumber(lxdh);
            threeForm.setReportTime(tbsj);
            threeFormMapper.update(threeForm);
            json.setMsg("ok");
            json.setFlag(0);

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
/*
删除
 */
    public ToJson<ThreeForm> deleteByPrimaryKey(Integer lid){
        ToJson<ThreeForm> json = new ToJson<>();
        try {
            threeFormMapper.deleteByPrimaryKey(lid);
            json.setMsg("ok");
            json.setFlag(0);

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
/*
详情
 */
    public ToJson<ThreeForm> selectByPrimaryKey(Integer lid){
        ToJson<ThreeForm> json = new ToJson<>();
        try {
            ThreeForm threeForm = threeFormMapper.selectByPrimaryKey(lid);
            json.setObject(threeForm);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;

    }
    /*
    列表
     */
    public ToJson<ThreeForm> query(Map<String,Object> maps, Integer page,
                                   Integer pageSize, boolean useFlag){
        ToJson<ThreeForm> json = new ToJson<>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        maps.put("page", pageParams);
        try {
            List<ThreeForm> threeFormList = threeFormMapper.query(maps);
            json.setObj(threeFormList);
            json.setTotleNum(pageParams.getTotal());
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }

        return json;
    }

}
